package org.hyn.gateway.assist.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.hyn.gateway.assist.GatewayException;
import org.hyn.gateway.assist.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author franyhu
 * @description 网关注册服务
 * @date 2025/3/26
 */
public class RegisterGatewayService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void doRegister(String address, String groupId, String gatewayId, String gatewayName, String gatewayAddress) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("groupId", groupId);
        params.put("gatewayId", gatewayId);
        params.put("gatewayName", gatewayName);
        params.put("gatewayAddress", gatewayAddress);
        String resultStr = HttpUtil.post(address, params, 350);
        Result result = JSON.parseObject(resultStr, Result.class);
        logger.info("向网关中心注册网关算力服务 gatewayId：{} gatewayName：{} gatewayAddress：{} 注册结果：{}", gatewayId, gatewayName, gatewayAddress, resultStr);
        if (!"0000".equals(result.getCode())) {
            throw new GatewayException("网关服务注册异常 [gatewayId：" + gatewayId + "] , [gatewayAddress：" + gatewayAddress + "]");
        }
    }
}
